<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">
          <img src="http://likede2-admin.itheima.net/img/logo.595745bd.png" alt="">
        </h3>
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          placeholder="请输入账号"
          name="username"
          type="text"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="password"
          v-model="loginForm.password"
          :type="type"
          placeholder="请输入密码"
          name="password"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="`${type === 'password' ? 'eye' : 'eye-open'}`" />
        </span>
      </el-form-item>
      <el-form-item prop="code" class="item-yanzhengma">
        <span class="svg-container">
          <svg-icon icon-class="link" />
        </span>
        <el-input
          ref="code"
          v-model="loginForm.code"
          placeholder="请输入验证码"
          name="yanzhengma"
          type="text"
        />
        <img :src="imgSrc" alt="" @click="getCode">
      </el-form-item>
      <el-button v-loading="loading" class="login-btn" type="primary" style="width:100%;margin-bottom:30px;" @click="submit">登录</el-button>
    </el-form>
  </div>
</template>

<script>

import { getCodeAPI } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        mobile: '',
        code: '',
        clientToken: Math.random(),
        loginType: 0,
        account: ''
      },
      loginRules: {
        loginName: [{ required: true, trigger: 'blur', message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      type: 'password',
      imgSrc: '',
      loading: false // 按钮加个loading
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    showPwd() {
      // 改变type 状态
      this.type === 'password' ? this.type = '' : this.type = 'password'
      // 点击聚焦，获取输入框，记得dom更新数据是异步的
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async getCode() {
      this.imgSrc = ''
      const res = await getCodeAPI(this.loginForm.clientToken)
      // console.log(res)
      // const blob = new Blob([res.data], { type: 'image/png' })
      // const url = window.URL.createObjectURL(blob)
      this.imgSrc = res.request.responseURL
      this.code = this.imgSrc
    },
    submit() {
      this.$refs.loginForm.validate(async(vali) => {
        try {
          // console.log(vali) // vali为true表示验证通过，false表示验证失败
          this.loading = true
          await this.$store.dispatch('user/loginAction', this.loginForm)
        } finally {
          this.loading = false
        }
        // 跳转首页
        this.$router.push('/')
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#fff;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background: url(~@/assets/bg.jpg) no-repeat;
  background-position: center;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
     height: 47px;
    background: transparent;
    border: 0;
    border-radius: 0;
    padding: 12px 5px 12px 15px;
    color: #999;
    caret-color: #999;
    -webkit-appearance: none;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
   width: 100%;
    background: #fff;
    border: 1px solid #e2e2e2;
    border-radius: 4px;
  }
  .el-form-item__error {
    color: red;
  }
  .login-btn{
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf
  }
  .item-yanzhengma{
    .el-form-item__content{
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;

  .login-form {
   position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 63px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    background-color: $bg;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      background-color: #fff;
      img {
    position: absolute;
    width: 96px;
    height: 96px;
    top: -168px;
    left: 50%;
    margin-left: -48px;
      }

    }
  }
  .show-pwd {

    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
